//
//  XMLoginView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/8.
//

#import "XMLoginView.h"
#import "XMConfirmServiceView.h"

@interface XMLoginView ()

@property (nonatomic, strong) UILabel *serviceLabel;
@property (nonatomic, strong) UILabel *phoneLabel;

@property (nonatomic, strong) UIButton *selectBtn;

@property (nonatomic, copy) NSString *phoneNumer;

@end

@implementation XMLoginView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
//    self.backgroundColor = kColorHEX(0xffffff);
    UIImageView *bodyImgView = [[UIImageView alloc] initWithFrame:self.bounds];
    bodyImgView.contentMode = UIViewContentModeScaleToFill;
    bodyImgView.image = ImageNamed(@"login_bg_img");
    [self addSubview:bodyImgView];
    
    _phoneNumer = @"13522446643";
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), kNavagationBarH + KScaleWidth(5.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(38.0)) font:KFontSize(20.0) textColor:kColorHEX(0x333333)];
    titleLabel.text = @"登录解锁更多精彩内容";
    [self addSubview:titleLabel];
    [self addSubview:self.serviceLabel];
    _serviceLabel.y = titleLabel.maxY + KScaleWidth(30.0);
    _serviceLabel.text = @"中国移动提供认证服务";
    
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), _serviceLabel.maxY, _serviceLabel.width, KScaleWidth(29.0)) font:KFontSize(16.0) textColor:kColorHEX(0x988DA2)];
    explainLabel.text = @"未注册手机号验证后将自动注册";
    [self addSubview:explainLabel];
    [self addSubview:self.phoneLabel];
    _phoneLabel.y = explainLabel.maxY + KScaleWidth(8.0);
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_phoneNumer];
//    [attrStr replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
//    [_phoneLabel setAttributedText:attrStr];
    _phoneLabel.text = _phoneNumer;
    
    //登录按钮
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake(KScaleWidth(28.0), _phoneLabel.maxY + KScaleWidth(55.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0)) title:@"本机号码一键登录" titleColor:kColorHEX(0x333333)];
    [loginBtn setCornerRadius:loginBtn.height/2];
    loginBtn.backgroundColor = kColorHEX(0xFFE44E);
    [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginBtn];
    
    UIButton *otherPhoneBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(KScaleWidth(28.0), loginBtn.maxY + KScaleWidth(16.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0)) title:@"其他手机号登录" titleColor:kColorHEX(0xCBC3D2)];
    [otherPhoneBtn setCornerRadius:otherPhoneBtn.height/2];
    otherPhoneBtn.backgroundColor = kColorHEX(0xF5F3F7);
    [otherPhoneBtn addTarget:self action:@selector(otherPhoneAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:otherPhoneBtn];
    [self addSubview:self.selectBtn];
    _selectBtn.y = otherPhoneBtn.maxY + KScaleWidth(27.0);
    
    //服务条款
    UILabel *serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(50.0), _selectBtn.y, self.width - KScaleWidth(50.0), _selectBtn.height)];
    NSMutableAttributedString *serviceAttr = [[NSMutableAttributedString alloc] initWithString:@"登录表示您已同意本平台《服务条款》和《隐私政策》" attributes:@{NSFontAttributeName : KFontSize(13.0)}];
    [serviceAttr setAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(13.0)} range:NSMakeRange(11, 5)];
    [serviceAttr setAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(13.0)} range:NSMakeRange(18, 5)];
    [serviceLabel setAttributedText:serviceAttr];
    [self addSubview:serviceLabel];
    
    CGFloat width = [@"登录表示您已同意本平台" getWidthWithFontSize:KFontSize(13.0)];
    
    UIButton *serviceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    serviceBtn.frame = CGRectMake(serviceLabel.x + width, serviceLabel.y, KScaleWidth(90.0), serviceLabel.height);
    [serviceBtn addTarget:self action:@selector(serviceCheckAction:) forControlEvents:UIControlEventTouchUpInside];
//    serviceBtn.backgroundColor = kColorHEX(0xff00ff);
    [self addSubview:serviceBtn];
    UIButton *privacyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    privacyBtn.frame = CGRectMake(serviceBtn.maxX + KScaleWidth(20.0), serviceBtn.y, serviceBtn.width, serviceBtn.height);
//    privacyBtn.backgroundColor = kColorHEX(0xff00ff);
    [privacyBtn addTarget:self action:@selector(privacyCheckAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:privacyBtn];
    
    UIButton *lookAroundBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(KScaleWidth(100.0), self.height - KScaleWidth(100.0) - KScaleWidth(40.0), self.width - KScaleWidth(100.0)*2, KScaleWidth(40.0)) title:@"随便看看" titleColor:kColorHEX(0x333333) image:@"login_next_icon"];
    [lookAroundBtn addTarget:self action:@selector(lookAroundAction:) forControlEvents:UIControlEventTouchUpInside];
    [lookAroundBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(7.0)];
    [self addSubview:lookAroundBtn];
    
//    UIButton *loginQQBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake((self.width - KScaleWidth(60.0)*2 - KScaleWidth(40.0))/2, lookAroundBtn.y - KScaleWidth(60.0) - KScaleWidth(20.0), KScaleWidth(60.0), KScaleWidth(60.0)) image:@"login_qq_icon" selectImg:@"login_qq_icon"];
//    [loginQQBtn addTarget:self action:@selector(loginQQAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:loginQQBtn];
    
//    UIButton *weLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(loginQQBtn.maxX + KScaleWidth(40.0), loginQQBtn.y, loginQQBtn.width, loginQQBtn.height) image:@"login_wechat_icon" selectImg:@"login_wechat_icon"];
    UIButton *weLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake((self.width - KScaleWidth(60.0))/2, lookAroundBtn.y - KScaleWidth(60.0) - KScaleWidth(20.0), KScaleWidth(60.0), KScaleWidth(60.0)) image:@"login_wechat_icon" selectImg:@"login_wechat_icon"];
    [weLoginBtn addTarget:self action:@selector(loginWithWe:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:weLoginBtn];
}

//本机号码一键登录
- (void)loginAction:(UIButton *)button {
    if (!_selectBtn.selected) {
        XMConfirmServiceView *confirmView = [[XMConfirmServiceView alloc] initWithFrame:self.bounds];
        [self addSubview:confirmView];
        [confirmView show];
        XMWeakSelf;
        confirmView.confirmBlock = ^{
            weakSelf.selectBtn.selected = YES;
            if ([weakSelf.delegate respondsToSelector:@selector(loginWithPhone:)]) {
                [weakSelf.delegate loginWithPhone:weakSelf.phoneNumer];
            }
        };
        confirmView.checkServiceBlock = ^(NSString * _Nonnull type) {
            if ([type isEqualToString:@"4"]) {
                [weakSelf serviceCheckAction:nil];
            }
            else {
                [weakSelf privacyCheckAction:nil];
            }
        };
        return;
    }
    if ([self.delegate respondsToSelector:@selector(loginWithPhone:)]) {
        [self.delegate loginWithPhone:_phoneNumer];
    }
}

//其他手机号登录
- (void)otherPhoneAction:(UIButton *)button {
    if (!_selectBtn.selected) {
        XMConfirmServiceView *confirmView = [[XMConfirmServiceView alloc] initWithFrame:self.bounds];
        [self addSubview:confirmView];
        [confirmView show];
        XMWeakSelf;
        confirmView.confirmBlock = ^{
            weakSelf.selectBtn.selected = YES;
            if ([weakSelf.delegate respondsToSelector:@selector(loginWithOtherPhone)]) {
                [weakSelf.delegate loginWithOtherPhone];
            }
        };
        confirmView.checkServiceBlock = ^(NSString * _Nonnull type) {
            if ([type isEqualToString:@"4"]) {
                [weakSelf serviceCheckAction:nil];
            }
            else {
                [weakSelf privacyCheckAction:nil];
            }
        };
        return;
    }
    if ([self.delegate respondsToSelector:@selector(loginWithOtherPhone)]) {
        [self.delegate loginWithOtherPhone];
    }
}

- (void)selectAction:(UIButton *)button {
    button.selected = !button.selected;
}

//服务条款
- (void)serviceCheckAction:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(checkService)]) {
        [self.delegate checkService];
    }
}

//隐私政策
- (void)privacyCheckAction:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(checkPrivacy)]) {
        [self.delegate checkPrivacy];
    }
}

- (void)lookAroundAction:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(lookAround)]) {
        [self.delegate lookAround];
    }
}

//QQ登录
- (void)loginQQAction:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(loginWithQQ)]) {
        [self.delegate loginWithQQ];
    }
}

//微信登陆
- (void)loginWithWe:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(loginWithWechat)]) {
        [self.delegate loginWithWechat];
    }
}

#pragma mark - Init
- (UILabel *)serviceLabel {
    if (!_serviceLabel) {
        _serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(33.0)) font:KFontSize(16.0) textColor:kColorHEX(0x988DA2)];
    }
    return _serviceLabel;
}

- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(83.0)) font:KBoldFontSize(23.0) textColor:kColorHEX(0x333333)];
        _phoneLabel.numberOfLines = 0;
    }
    return _phoneLabel;
}

- (UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(23.0), 0, KScaleWidth(26.0), KScaleWidth(26.0)) image:@"login_service_select_icon" selectImg:@"login_service_select_icon_selected"];
        [_selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
